package engine;

import java.io.File;
import creatures.Creature;
import creatures.Monster;
import creatures.Player;
import engine.display.Display;
import engine.display.Input;
import engine.display.Messenger;
import engine.map.Map;
import engine.map.staticobjects.Wall;
import engine.time.Time;
import engine.utils.Utils;

/**
 * The main class for this project.
 * 
 * @author Kyle_Solo
 */
public class Game {
	// public static final Game game = new Game();

	/**
	 * The Game's Display.
	 */
	public static Display display;

	/**
	 * The Game's Input.
	 */
	public static Input input;

	/**
	 * The Game's current Map.
	 */
	public static Map map;

	/**
	 * The Game's current messenger.
	 */
	public static Messenger messenger;

	/**
	 * This variable controls the Game's state. If set to false, the game will end shortly
	 * thereafter.
	 */
	public static boolean running = true;

	/**
	 * The Game's time system.
	 */
	public static Time time;

	private static void gameLoop() {
		while (running) {
			display.render();
			Creature c = time.next();
			if (c != null) {
				/*
				 * Update the creature, store how long the creature's action will take.
				 */
				int timeTaken = c.update();
				if (time.contains(c)) {
					// update the time system if needed
					time.act(c, timeTaken);
				}
			}
		}
	}

	/**
	 * Sets up the Game class, initializing static variables.
	 */
	public static void initGame() {
		display = new Display(200, 60);
		input = display.getInput();
		messenger = new Messenger();
		map = new Map(200, 60);
		time = new Time();
		// add some random walls
		for (int i = 0; i < 50; i++) {
			map.setTile(Utils.randomInt(0, 199), Utils.randomInt(0, 59), new Wall());
		}
		new Player(20, 40);
		new Monster(40, 40);

		// clear the drawing canvas
		display.cls();
		// output the canvas to the screen
		display.render();
	}

	/**
	 * Loads the game
	 */
	public static void load() {
		time.clear();
		// get rid of existing dynamic objects
		Creature.clearCreatures();

		map.load(".\\save\\");

		Persistent.load(".\\save\\");
	}

	/**
	 * This is the entry point into the game. Code execution starts here.
	 * 
	 * @param args
	 *            command line arguments
	 */
	public static void main(String[] args) {
		Game.initGame();
		gameLoop();
		System.exit(0);
	}

	/**
	 * Saves the game
	 */
	public static void save() {
		File directory = new File(".\\save\\");
		if (!directory.exists()) {
			if (!directory.mkdir()) {
				System.err.println("Unable to create save directory");
			}
		} else {
			System.out.println("Deleting old save...");
			File files[] = directory.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					files[i].delete();
					/*
					 * todo: potentially unsafe deletion of old saves, use an index file instead?
					 */
				}
			}
		}
		map.save(".\\save\\");
		Persistent.save(".\\save\\");
	}
}
